package com.itdct.mto.core.manager;

import com.itdct.mto.core.bo.IndexBo;
import com.itdct.mto.core.bo.TableBo;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Zhouwx
 * @date 2025/7/25 14:53:05
 * @version 1.0
 * @description
 */
@ConditionalOnProperty(name = "mto.database-mode", havingValue = "PW")
@Slf4j
@Component
public class PanweiTableConvertManager extends BTableConvertManager{
    @Override
    public String generateTableDdl(TableBo tableBo) {
        String tableDdl = tableBo.getTableDdl();
        String[] lines = tableDdl.split("\n");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            // INFO: Zhouwx: 2025/7/25 统一编码格式到utf8，防止乱七八糟的编码格式导致磐维报错
            if (line.contains("CHARACTER SET") && line.contains("COLLATE")) {
                String[] splitMember = line.split(" ");
                for (int j = 0; j <splitMember.length; j++) {
                    String word = splitMember[j];
                    if (word.equals("CHARACTER")) {
                        int charsetIndex = j + 2;
                        splitMember[charsetIndex] = "utf8mb4";
                    }

                    if (word.equals("COLLATE")) {
                        int collateIndex = j + 1;
                        splitMember[collateIndex] = "utf8mb4_general_ci";
                    }
                }
                lines[i] = String.join(" ", splitMember);
            }

            if (line.contains("CHARSET=") && line.contains("COLLATE=")) {
                String[] split = line.split(" ");
                for (int j = 0; j < split.length; j++) {
                    String word = split[j];
                    if (word.contains("CHARSET=")) {
                        String[] pairArray = word.split("=");
                        pairArray[1]="utf8mb4";
                        split[j] = String.join("=", pairArray);
                    }

                    if (word.contains("COLLATE=")) {
                        String[] pairArray = word.split("=");
                        pairArray[1]="utf8mb4_general_ci";
                        split[j] = String.join("=", pairArray);
                    }
                }
                lines[i] = String.join(" ", split);
            }
        }
        tableDdl = String.join("\n", lines);

        // INFO: Zhouwx: 2025/7/7 索引名必须要修改，不然进入OpenGauss是全局索引，索引名称重复会报错
        ArrayList<IndexBo> indexList = tableBo.getIndexList();
        String tableName = tableBo.getTableName();
        for (IndexBo indexBo : indexList) {
            String indexType = indexBo.getIndexType();
            if (indexType.equals("KEY") || indexType.equals("UNIQUE KEY")) {
                String indexName = indexBo.getIndexName().replace("`", "").trim();
                String pgIndexName = tableName + "__" + indexName;
                if (pgIndexName.length() > 63) {
                    pgIndexName = pgIndexName.substring(pgIndexName.length() - 63);
                }
                tableDdl = tableDdl.replace("`" + indexName + "`", "`" + pgIndexName + "`");
            }

        }

        if (tableBo.isPartitionTable()) {
            // INFO: Zhouwx: 2025/10/17 分区表需要专门处理
            if (tableDdl.contains("/*!50100 PARTITION BY")) {
                int i = tableDdl.indexOf("/*!50100 PARTITION BY");
                tableDdl = tableDdl.substring(0, i);
            }
            String partitionInfo = partitionBy(tableBo);
            tableDdl+= partitionInfo;
        }
        return tableDdl;
    }

}
